//
//  ZYAlterMessageView.m
//  YiLin
//
//  Created by Zyark on 2018/6/8.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYAlterMessageView.h"
#import "ZYImageUtils.h"

@interface ZYAlterMessageView()
@property(nonatomic,strong) UIView *bigBgView;
@property(nonatomic,assign) BOOL isAlterSelectView;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UILabel *messageLabel;

/**
 取消
 */
@property(nonatomic,strong) UIButton *cancelBtn;

/**
 去链接
 */
@property(nonatomic,strong) UIButton *confirmBtn;
@end

@implementation ZYAlterMessageView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _bigBgView = [[UIView alloc] initWithFrame:self.bounds];
    _bigBgView.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self addSubview:_bigBgView];
    
    CGFloat width = self.width * 0.72;
    CGFloat height = width * 0.68;
    if([[language userLanguage] containsString:@"zh"]){
        height = width * 0.5;
    }
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    _bgView.centerX = self.centerX;
    _bgView.centerY = self.centerY - _bgView.height / 4;
    
    CGFloat margin = (0.073 *_bgView.width);
    CGFloat btnWidth = (_bgView.width - 3 * margin)/2;
    _cancelBtn = [[UIButton alloc] init];
    _cancelBtn.size = CGSizeMake(btnWidth, btnWidth * 0.3);
    [_cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [_cancelBtn setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
    [_cancelBtn setBackgroundImage:[ZYImageUtils imageWithColor:RGBA(235, 235, 235, 1) rect:_cancelBtn.bounds] forState:UIControlStateNormal];
    [_bgView addSubview:_cancelBtn];
    _cancelBtn.left = margin;
    _cancelBtn.bottom = _bgView.height - 14;
    [_cancelBtn setTitle:[language getStringByKey:LC_DEVICE_ADD_REMIND_BTN_CANCEL] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    _cancelBtn.layer.cornerRadius = 5;
    _cancelBtn.layer.masksToBounds = YES;
    
    _confirmBtn = [[UIButton alloc] init];
    _confirmBtn.size = CGSizeMake(btnWidth, btnWidth * 0.3);
    [_confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_confirmBtn setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:_cancelBtn.bounds] forState:UIControlStateNormal];
    [_bgView addSubview:_confirmBtn];
    _confirmBtn.left = _cancelBtn.right + margin;
    _confirmBtn.bottom = _cancelBtn.bottom;
    [_confirmBtn setTitle:[language getStringByKey:LC_DEVICE_ADD_REMIND_BTN_LINK] forState:UIControlStateNormal];
    [_confirmBtn addTarget:self action:@selector(doConfirm) forControlEvents:UIControlEventTouchUpInside];
    _confirmBtn.layer.cornerRadius = 5;
    _confirmBtn.layer.masksToBounds = YES;
    
    _messageLabel = [[UILabel alloc] init];
    _messageLabel.textColor = kLabelColor;
    _messageLabel.textAlignment = NSTextAlignmentLeft;
    _messageLabel.font = [UIFont systemFontOfSize:14];
    _messageLabel.size = CGSizeMake(_bgView.width - margin * 2, _cancelBtn.top  - margin - 5);
    _messageLabel.numberOfLines = 0;
    [_bgView addSubview:_messageLabel];
    _messageLabel.centerX = _bgView.width /2;
    _messageLabel.top = margin;
//    _messageLabel.text = @"No WiFi available at the moment. Please leave the phone and thermostat under the same WiFi";
    _messageLabel.text = [language getStringByKey:LC_DEVICE_ADD_REMIND_LABEL_MESSAGE];
    
    self.alpha = 0;
    self.bigBgView.hidden = YES;
    self.top = -self.height;
    self.backgroundColor = [UIColor clearColor];
}

-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)doConfirm{
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    }
}
@end
